<?php
/**
 * 漳州兴业银行支付
 */
namespace app\components\pay;

use Yii;

use app\components\Func;
use app\components\KelaileJava;

use app\models\Order;
use app\models\OrderPay;

class CibPay extends Pay {

    const PREPAY_URL = 'superpay/nativepay'; //主扫
    const QRPAY_URL = 'superpay/micropay'; //被扫
    const REFUND = 'superpay/refund'; //退款

    public $partnerId; //商户号
    public $partnerKey;//商户key

    public function __construct($partnerId = '', $partnerKey = '') {
        $this->partnerId = $partnerId;
        $this->partnerKey = $partnerKey;
    }

    //获取支付成功通知url
    public function notifyurl(){
        return Func::getHostInfo() . '/' . KelaileJava::NOTIFY_URL;
    }

    //刷卡支付
    public function micropay(array $params) {
        if(!isset($params['device_no'])) throw new \Exception('device_no为空');
        if(!isset($params['out_trade_no'])) throw new \Exception('out_trade_no为空');
        if(!isset($params['total_fee'])) throw new \Exception('total_fee为空');
        if(!isset($params['auth_code'])) throw new \Exception('auth_code为空');
        if(!isset($params['body'])) throw new \Exception('body为空');
        $params['notify_url'] = $this->notifyurl();
        $params['account_info'] = [
            $params['cib_pay_type'] => [
                'mch_id' => $this->partnerId,
                'mch_key' => $this->partnerKey,
            ],
        ];
        $javaRes = self::getResBuyJavaApi(self::QRPAY_URL, $params);
        return $javaRes;
    }

    /**
     * 支付成功
     * @param array $postArr java返回的数据
     */
    public static function paySuccess($postArr, $order) {
        $resArr = $postArr;
        if($postArr['pay_type'] == KelaileJava::CIB_WX_PAY) $payType = OrderPay::PAY_TYPE_CIB_WX;
        if($postArr['pay_type'] == KelaileJava::CIB_ALI_PAY) $payType = OrderPay::PAY_TYPE_CIB_ALI;
        $order->pay_type = $payType;
        $order->status = ($resArr['pay_status'] == '1') ? Order::STATUS_PAY_END : Order::STATUS_PAY_FAIL;
        //$order->out_trade_no = $resArr['transaction_id']; //交易流水号
        $order->ptime = $resArr['pay_time'];
        $order->third_discount_fee += $resArr['ent_discount_fee']; //商家优惠
        $order->third_pay_discount_fee += $resArr['discount_fee']; //支付平台补贴
        $order->saveRedis();

        if($resArr['pay_status'] != '1') throw new \Exception($resArr['pay_msg']);

        $orderPay = new OrderPay();
        $orderPay->order_id = $order->id;
        $orderPay->enterprise_id = $order->enterprise_id;
        $orderPay->shop_id = $order->shop_id;
        $orderPay->befrom = $order->befrom;
        $orderPay->device_no = $order->device_no;
        $orderPay->cashier_id = $order->cashier_id;
        $orderPay->pay_type = $payType;
        $orderPay->pay_fee = $resArr['receipt_amount']; //商家实收金额
        $orderPay->payee = $resArr['account'];  //收款人
        $orderPay->payer = $resArr['payer'];    //付款人
        $orderPay->trade_id = $resArr['transaction_id'];//交易流水号
        $orderPay->utime = $resArr['pay_time'];
        $orderPay->discount_fee = $order->discount_fee;
        $orderPay->third_discount_fee = $order->third_discount_fee;
        $orderPay->saveRedis();
    }

    public function prepay(array $paras) {

    }
    public function orderquery(array $paras) {

    }

    //退款
    public function refund(array $params) {
        if(!isset($params['out_trade_no'])) throw new \Exception('out_trade_no为空');
        if(!isset($params['refund_amount'])) throw new \Exception('refund_amount为空');
        if(!isset($params['refund_reason'])) throw new \Exception('refund_reason为空');  //退款原因:设备号,标识客来乐设备退款
        $javaRes = self::getResBuyJavaApi(self::REFUND, $params);
        if($javaRes['res']['status'] != '1') throw new \Exception($javaRes['res']['sub_msg']);
        return [
            'refund_trade_no' => $javaRes['res']['refund_trade_no'], //java生成
            'refund_trade_id' => $javaRes['res']['transaction_id'], //退款交易流水号
        ];
    }

    public function refundquery(array $paras) {

    }
    public function cancel(array $paras) {

    }
    public function getSign($paras) {

    }
    public function checkNotifySign($paras) {

    }

}
